import axios from "axios";
import { ElMessage } from 'element-plus'
import router from '@/router/index'
import { createRouter, createWebHistory } from 'vue-router';


// import { getCookie } from "./cookie";
// import Cookies from "js-cookie"
const service = axios.create({
    baseURL: "http://127.0.0.1:7001",
    timeout: 5000
})
service.interceptors.request.use(config => {
    // console.log(Cookies,"getcookie()");
    // const token = getCookie();
    // console.log(token);
    let token=""
    if(window.localStorage.getItem("userinfo")){
 let obj=JSON.parse(window.localStorage.getItem("userinfo"));
 token=obj.token
   }
    if (token) {
        config.headers.token = token;
    }
    return config;
},
    error => {
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response=>{
        return response.data
    },
    error=>{
        console.log(router,"trouter")
    console.log(error,"error")
    if(error.response.data.code == 400){
        ElMessage({
            type:"warning",
            message:error.response.data.msg
        })
    }else if(error.response.status == 401){
       
        ElMessage({
            type:"warning",
            message:error.response.data.data
        })
      
        router.push({
            path:"/"
        })

    }
        return Promise.reject(error)
    
    }
)
export default service